/*
 * $Id$
 *
 * Copyright ( c ) 2014 Carefx Corporation. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of Carefx
 * Corporation ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with Carefx Corporation or a Carefx
 * authorized reseller (the "License Agreement"). Carefx may make changes to the
 * Confidential Information from time to time. Such Confidential Information may
 * contain errors.
 *
 * EXCEPT AS EXPLICITLY SET FORTH IN THE LICENSE AGREEMENT, CAREFX DISCLAIMS ALL
 * WARRANTIES, COVENANTS, REPRESENTATIONS, INDEMNITIES, AND GUARANTEES WITH
 * RESPECT TO SOFTWARE AND DOCUMENTATION, WHETHER EXPRESS OR IMPLIED, WRITTEN OR
 * ORAL, STATUTORY OR OTHERWISE INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A
 * PARTICULAR PURPOSE. CAREFX DOES NOT WARRANT THAT END USER'S USE OF THE
 * SOFTWARE WILL BE UNINTERRUPTED, ERROR FREE OR SECURE.
 *
 * CAREFX SHALL NOT BE LIABLE TO END USER, OR ANY OTHER PERSON, CORPORATION OR
 * ENTITY FOR INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL
 * DAMAGES, OR DAMAGES FOR LOSS OF PROFITS, REVENUE, DATA OR USE, WHETHER IN AN
 * ACTION IN CONTRACT, TORT OR OTHERWISE, EVEN IF CAREFX HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES. CAREFX' TOTAL LIABILITY TO END USER SHALL NOT
 * EXCEED THE AMOUNTS PAID FOR THE CAREFX SOFTWARE BY END USER DURING THE PRIOR
 * TWELVE (12) MONTHS FROM THE DATE IN WHICH THE CLAIM AROSE.  BECAUSE SOME
 * STATES OR JURISDICTIONS DO NOT ALLOW LIMITATION OR EXCLUSION OF CONSEQUENTIAL
 * OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO END USER.
 *
 * Copyright version 2.0
 */
package com.th.supcom.pds.prescstatusquery.client.widget;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.th.supcom.pds.common.client.model.PdsSupplyModel;

/**
 * 处方状态查询界面
 */
public class SupplyStatusQueryPanel extends LayoutContainer
{
    private Component m_component;
    private final PdsSupplyModel m_currentCondition;
    private Component m_focusComponent;
    private SupplyStatusQueryPatientInfoPanel m_prescstatusPatientInfoPanel;
    private SupplyStatusQueryAction m_prescStatusQueryAction;
    private SupplyStatusQueryButtons m_prescStatusQueryButtons;
    private SupplyStatusQueryDetailGrid m_prescStatusQueryDetailGrid;
    private SupplyStatusQueryFields m_prescStatusQueryFields;
    private SupplyStatusQueryMainGrid m_prescStatusQueryMainGrid;
    private SupplyStatusQueryPrescGrid m_prescStatusQueryPrescGrid;

    /**
     * 构造函数
     * 
     * @param currentCondition
     */
    public SupplyStatusQueryPanel (final PdsSupplyModel currentCondition)
    {
        m_currentCondition = currentCondition;
        initUI ();
    }

    /**
     * 构造函数
     * 
     * @param currentCondition
     * @param component
     */
    public SupplyStatusQueryPanel (final PdsSupplyModel currentCondition, final Component component)
    {
        m_currentCondition = currentCondition;
        m_component = component;
        initUI ();
    }

    /**
     * 构造函数
     * 
     * @param currentCondition
     * @param component
     * @param focusComponent
     */
    public SupplyStatusQueryPanel (final PdsSupplyModel currentCondition, final Component component, final Component focusComponent)
    {
        m_currentCondition = currentCondition;
        m_component = component;
        m_focusComponent = focusComponent;
        initUI ();
    }

    /**
     * 初始化事件
     */
    private void initAction ()
    {
        m_prescStatusQueryAction = new SupplyStatusQueryAction ();
        m_prescStatusQueryAction.setPrescStatusQueryDetailGrid (m_prescStatusQueryDetailGrid);
        m_prescStatusQueryAction.setPrescStatusQueryPrescGrid (m_prescStatusQueryPrescGrid);
        m_prescStatusQueryAction.setPrescStatusQueryFields (m_prescStatusQueryFields);
        m_prescStatusQueryAction.setSupplyStatusQueryPanel (this);
    }

    /**
     * 初始化面板组件
     */
    private void initComponents ()
    {
        m_prescStatusQueryButtons = new SupplyStatusQueryButtons ();
        m_prescStatusQueryFields = new SupplyStatusQueryFields ();
        m_prescStatusQueryDetailGrid = new SupplyStatusQueryDetailGrid ();
        m_prescStatusQueryPrescGrid = new SupplyStatusQueryPrescGrid ();
        m_prescStatusQueryMainGrid = new SupplyStatusQueryMainGrid (m_prescStatusQueryPrescGrid, m_prescStatusQueryDetailGrid);
        m_prescstatusPatientInfoPanel = new SupplyStatusQueryPatientInfoPanel (m_prescStatusQueryFields, m_prescStatusQueryButtons);
    }

    /**
     * 初始化数据
     */
    private void initData ()
    {
        if (null == m_currentCondition)
        {
            return;
        }

        final String prescNo = m_currentCondition.getPrescNo ();
        final String rcptNo = m_currentCondition.getRcptNo ();

        if (null == prescNo && null == rcptNo)
        {
            return;
        }
        m_prescStatusQueryAction.findPrescByCondition (prescNo, rcptNo);
    }

    /**
     * 初始化获取焦点
     */
    private void initFocus ()
    {
        m_prescStatusQueryFields.getConsumeProofField ().focus ();
    }

    /**
     * 初始化事件监听器
     */
    private void initListener ()
    {
        final SupplyStatusQueryListener prescStatusQueryListener = new SupplyStatusQueryListener ();
        prescStatusQueryListener.setPrescStatusQueryAction (m_prescStatusQueryAction);
        prescStatusQueryListener.setPrescStatusQueryButtons (m_prescStatusQueryButtons);
        prescStatusQueryListener.setKeyNavComponent (m_component);
        prescStatusQueryListener.setPrescStatusQueryFields (m_prescStatusQueryFields);
        prescStatusQueryListener.setPrescStatusQueryPrescGrid (m_prescStatusQueryPrescGrid);
        prescStatusQueryListener.setFocusComponent (m_focusComponent);
        prescStatusQueryListener.initListeners ();
    }

    /**
     * 初始化UI
     */
    private void initUI ()
    {
        this.setLayout (new BorderLayout ());

        initComponents ();

        final BorderLayoutData centerData = new BorderLayoutData (LayoutRegion.CENTER);
        centerData.setMargins (new Margins (20, 5, 10, 5));
        final BorderLayoutData northData = new BorderLayoutData (LayoutRegion.NORTH, 150);
        northData.setMargins (new Margins (2, 5, 2, 5));

        this.add (m_prescstatusPatientInfoPanel, northData);
        this.add (m_prescStatusQueryMainGrid, centerData);

        initAction ();
        initListener ();
        initData ();
        initFocus ();
    }
}
